#include<iostream>
#include<iomanip>
using namespace std;

class MagicCube
{
private:
	int order;
	int* array;
public:
	MagicCube();
	MagicCube(int);
	void odd();
	void even();
	void oddEven();
	bool check();
	void display();
	~MagicCube();
};

MagicCube::MagicCube()
{
	order = 3;
	array = new int[order * order];
	if (!array)
	{
		cout << "内存申请错误！" << endl;
		system("pause");
		exit(1);
	}
}

MagicCube::MagicCube(int order)
{
	this->order = order;
	array = new int[order * order];
	if (!array)
	{
		cout << "内存申请错误！" << endl;
		system("pause");
		exit(1);
	}
}

void MagicCube::odd()
{
	int rowIndex = 0, colIndex = (order - 1) / 2;
	fill(array, array + order * order, 0);
	*(array + colIndex) = 1;

	for (int i = 2; i <= order * order; ++i)
	{
		if (rowIndex == 0 && colIndex != order - 1)
		{
			rowIndex = order - 1;
			colIndex++;
			*(array + rowIndex * order + colIndex) = i;
			continue;
		}

		if (rowIndex != 0 && colIndex == order - 1)
		{
			rowIndex--;
			colIndex = 0;
			*(array + rowIndex * order + colIndex) = i;
			continue;
		}

		if (rowIndex == 0 || colIndex == order - 1 || *(array + (rowIndex - 1) * order + colIndex + 1) != 0)
		{
			rowIndex++;
			*(array + rowIndex * order + colIndex) = i;
		}
		else
		{
			rowIndex--;
			colIndex++;
			*(array + rowIndex * order + colIndex) = i;
		}
	}
}

void MagicCube::even()
{
	int i, tmp, rowIndex, colIndex;
	for (i = 0; i < order * order; ++i)
	{
		*(array + i) = i + 1;
	}

	rowIndex = 0, colIndex = order / 4;
	for (i = 1; i <= (order / 2) * (order / 2); ++i)
	{
		if (i != 1 && i % (order / 2) == 1)
		{
			rowIndex++;
			colIndex = order / 4;
		}
		tmp = *(array + rowIndex * order + colIndex);
		*(array + rowIndex * order + colIndex) = *(array + (order - rowIndex - 1) * order + colIndex);
		*(array + (order - rowIndex - 1) * order + colIndex) = tmp;
		colIndex++;
	}

	rowIndex = order / 4, colIndex = 0;
	for (i = 1; i <= (order / 2) * (order / 2); ++i)
	{
		if (i != 1 && i % (order / 2) == 1)
		{
			rowIndex++;
			colIndex = 0;
		}
		tmp = *(array + rowIndex * order + colIndex);
		*(array + rowIndex * order + colIndex) = *(array + rowIndex * order + order - colIndex - 1);
		*(array + rowIndex * order + order - colIndex - 1) = tmp;
		colIndex++;
	}
}

void MagicCube::oddEven()
{

}

bool MagicCube::check()
{
	int rowIndex, colIndex;
	long rowSum, colSum, diaSum1, diaSum2;
	long sum = (order * (order * order + 1)) / 2;

	for (rowIndex = 0; rowIndex < order; ++rowIndex)
	{
		rowSum = 0;
		for (colIndex = 0; colIndex < order; ++colIndex)
		{
			rowSum += *(array + rowIndex * order + colIndex);
		}
		if (sum != rowSum)
		{
			return false;
		}
	}

	for (colIndex = 0; colIndex < order; ++colIndex)
	{
		colSum = 0;
		for (rowIndex = 0; rowIndex < order; ++rowIndex)
		{
			colSum += *(array + rowIndex * order + colIndex);
		}
		if (sum != colSum)
		{
			return false;
		}
	}

	diaSum1 = 0, diaSum2 = 0;
	for (rowIndex = 0, colIndex = order - 1; rowIndex < order; ++rowIndex, --colIndex)
	{
		diaSum1 += *(array + rowIndex * order + colIndex);
	}
	for (int i = 0; i < order; ++i)
	{
		diaSum2 += *(array + i * order + i);
	}
	if (sum != diaSum1 && sum != diaSum2)
	{
		return false;
	}
	else
	{
		return true;
	}
}

void MagicCube::display()
{
	for (int i = 0; i < order; ++i)
	{
		for (int j = 0; j < order; ++j)
		{
			int tmp = order * order, k;
			for (k = 0; tmp; ++k)
			{
				tmp /= 10;
			}
			cout << setfill('0') << setw(k) << *(array + i * order + j) << " ";
		}
		cout << endl;
	}
}

MagicCube::~MagicCube()
{
	delete[] array;
}

void main()
{
	int num;
	cout << "请输入魔方阶数：";
	cin >> num;
	MagicCube mc(num);

	if (num < 1)
	{
		cout << "您输入的数字没有意义！" << endl;
		system("pause");
		exit(0);
	}
	else if (num == 2)
	{
		cout << "您输入的数字无法构造魔方。" << endl;
		system("pause");
		exit(0);
	}
	else if (num % 2 != 0)
	{
		mc.odd();
	}
	else if (num % 4 == 0)
	{
		mc.even();
	}
	else
	{
		mc.oddEven();
	}
	if (mc.check())
	{
		mc.display();
	}
	else
	{
		cout << "构造的魔方错误！" << endl;
	}
	system("pause");
}